package com.aorise.service.camera.impl;

import com.aorise.exception.ServiceException;
import com.aorise.mapper.camera.CameraGroupMapper;
import com.aorise.mapper.system.UserMapper;
import com.aorise.model.camera.*;
import com.aorise.service.camera.CameraGroupService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:cat
 * @Desicription: 视频设备分组接口服务实现类
 * @Date:Created in 2019-06-14
 * @Modified By:
 */
@Service
public class CameraGroupServiceImpl implements CameraGroupService {

    @Autowired
    private CameraGroupMapper cameraGroupMapper;

    /**
     * @Author:cat
     * @Description 按层级查询所有视频设备分组信息
     * @Date:2019-06-14
     * @Return: List<CameraGroupModel> 视频设备分组信息集合
     * @Modified By:
     */
    @Override
    public List<CameraGroupModel> getAllCameraGroupListTree() {

        return getChildrenTree(0);
    }

    /**
     * 递归查询视频设备分组信息（树形）
     * @param parentId 父级ID
     * @return
     */
    public List<CameraGroupModel> getChildrenTree(Integer parentId) {
        List<CameraGroupModel> list = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        map.put("state", ConstDefine.STATE_ABLE);
        List<CameraGroupModel> children = cameraGroupMapper.getCameraGroupByParentId(map);
        for (CameraGroupModel cameraGroupModel : children) {
            CameraGroupModel model = new CameraGroupModel();
            model.setId(cameraGroupModel.getId());
            model.setGName(cameraGroupModel.getGName());
            model.setParentId(cameraGroupModel.getParentId());
            model.setCameraGroupModels(getChildrenTree(cameraGroupModel.getId()));
            list.add(model);
        }
        return list;
    }

    /**
     * @Author:cat
     * @Description 通过ID查询当前级到最父级的分组名称信息
     * @param id ID
     * @Date:2019-06-14
     * @Return: String 分组名称
     * @Modified By:
     */
    @Override
    public String getCameraGroupNameById(Integer id) {
        String groupName = "";
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("state", ConstDefine.STATE_ABLE);
        CameraGroupModel groupModel = cameraGroupMapper.getCameraGroupById(map);
        if(groupModel != null){
            groupName=groupModel.getGName();
            if(groupModel.getParentId() != 0){
                groupName=getCameraGroupNameById(groupModel.getParentId())+ "-" +groupName;
            }
        }
        return groupName;
    }

    /**
     * @Author:cat
     * @Description 根据ID查询视频设备分组详情
     * @params: id 主键ID
     * @Date:2019-06-14
     * @Return: CameraGroupModel 视频设备分组信息
     * @Modified By:
     */
    @Override
    public CameraGroupModel getCameraGroupById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("state", ConstDefine.STATE_ABLE);
        CameraGroupModel groupModel = cameraGroupMapper.getCameraGroupById(map);
        if(!groupModel.getParentId().toString().equals("0")){
            map.put("id", groupModel.getParentId());
            CameraGroupModel model = cameraGroupMapper.getCameraGroupById(map);
            groupModel.setParentParentId(model.getParentId());
        }
        return groupModel;
    }

}
